package org.mclll.xwangplugin.cartography.listener;

import com.bekvon.bukkit.residence.api.ResidenceApi;
import com.bekvon.bukkit.residence.protection.ClaimedResidence;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.CartographyInventory;
import org.bukkit.inventory.Inventory;
import org.mclll.xwangplugin.MclllCustom;
import org.mclll.xwangplugin.cartography.recipes.CartographyCustomRecipes;
import org.mclll.xwangplugin.cartography.recipes.CartographyTransferReelCopyRecipes;
import org.mclll.xwangplugin.cartography.recipes.CartographyTransferReelMainRecipes;
import org.mclll.xwangplugin.config.GlobalConfig;
import org.mclll.xwangplugin.log.GlobalLog;
import org.mclll.xwangplugin.log.LogUtil;
import org.mclll.xwangplugin.log.LoggerFactory;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author: geduo
 * @date: 2023/10/24 09:36
 */
public class CartographyListener implements Listener {

    private LogUtil log;
    private MclllCustom mclllCustom;
    private YamlConfiguration config;
    private final String configKey = "cartography";

    public CartographyListener(MclllCustom mclllCustom) {
        this.mclllCustom = mclllCustom;
        this.log = LoggerFactory.get(GlobalLog.class);
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onInventoryClickEventNormal(InventoryClickEvent event) {
        // 这里主要是为了添加制图台，非制图台直接return
        Inventory cartographyInventory = event.getInventory();
        if (!(event.getInventory() instanceof CartographyInventory)) {
            return;
        }
        Location location = cartographyInventory.getLocation();
        String worldName = location.getWorld().getName();
        ConfigurationSection config = GlobalConfig.CONFIG.getConfigurationSection(configKey);
        List<String> worldList = config.getStringList("worldList");
        if (config.getBoolean("worldBlack") ? checkInBlack(worldList, worldName) : checkNotInWhite(worldList, worldName)) {
            return;
        }
        List<String> resList = config.getStringList("resList");
        String resName = Optional.ofNullable(ResidenceApi.getResidenceManager().getByLoc(location))
                .map(ClaimedResidence::getName)
                .orElse("");
        if (config.getBoolean("resBlack") ? checkInBlack(resList, resName) : checkNotInWhite(resList, resName)) {
            return;
        }

        List<CartographyCustomRecipes> queue = Arrays.asList(new CartographyTransferReelCopyRecipes(), new CartographyTransferReelMainRecipes());
        for (CartographyCustomRecipes recipes : queue) {
            recipes.toCreate(event, cartographyInventory);
            if (event.isCancelled()) {
                return;
            }
        }
    }

    private boolean checkNotInWhite(List<String> list, String str) {
        for (String s : list) {
            if (str.matches(s)) {
                return false;
            }
        }
        return true;
    }
    private boolean checkInBlack(List<String> list, String str) {
        for (String s : list) {
            if (str.matches(s)) {
                return true;
            }
        }
        return false;
    }


}
